<?php

/*
 * 作者：亮剑
 * 联系QQ:578933760
 *
 */

if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

if(empty($_G['uid'])){
	showmessage(lang('plugin/aljbd','sc2'), '', array(), array('login' => true));
}

$_GET=dhtmlspecialchars($_GET);
$num = intval($_GET['num']);
$settings=C::t('#aljbd#aljbd_setting')->range();
$shop=C::t('#aljbd#aljbd_goods')->fetch($_GET['sid']);

$orderid = dgmdate(TIMESTAMP, 'YmdHis').random(18);
if(C::t('#aljbd#aljbd_order')->fetch($orderid)) {
	showmessage('credits_addfunds_order_invalid', '', array(), array('showdialog' => 1, 'showmsg' => true, 'closetime' => true));
}

if(empty($_G['uid'])){
	showmessage(lang('plugin/aljbd','sc2'), '', array(), array('login' => true));
}

if ($_GET['formhash'] != formhash() || !submitcheck('formhash', 1) || preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) != preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) {
	debug('Access Denied!');
}

$shop = C::t('#aljbd#aljbd_goods')->fetch($_GET['sid']);

if (empty($num)) {
	showmessage(lang('plugin/aljbd','sc16'));
}
if ($shop['amount'] < $num) {
	showmessage(lang('plugin/aljbd','sc17'));
}

if (TIMESTAMP > $shop['endtime'] && !empty($shop['endtime'])) {
	echo showmessage(lang('plugin/aljbd','sc19'));
	exit;
}

C::t('#aljbd#aljbd_goods')->update_num_by_id($_GET['sid'],$num);

$allattrs = C::t('#aljbd#aljbd_attr') -> range();
foreach($_GET['typename'] as $v){
	$attrs[]=$allattrs[$v]['content'];
}
$attrs = implode(',',$attrs);
//手机支付宝支付为payment=2
C::t('#aljbd#aljbd_order')->insert(array(
	'orderid' => $orderid,
	'status' => '1',
	'uid' => $_G['uid'],
	'username' => $_G['username'],
	'sid' => $_GET['sid'],
	'stitle' => $shop['name'],
	'amount' => $num,
	'price' => $shop['price1'],
	'submitdate' => $_G['timestamp'],
	'remarks' => $attrs.$_GET['remarks'],
	'payment' => 2,
));






$alipay_config['partner']		=   trim($settings['ec_partner']['value']);

//安全检验码，以数字和字母组成的32位字符
//如果签名方式设置为“MD5”时，请设置该参数
$alipay_config['key']			= $settings['ec_securitycode']['value'];

//商户的私钥（后缀是.pen）文件相对路径
//如果签名方式设置为“0001”时，请设置该参数
$alipay_config['private_key_path']	= 'source/plugin/aljbd/key/rsa_private_key.pem';

//支付宝公钥（后缀是.pen）文件相对路径
//如果签名方式设置为“0001”时，请设置该参数
$alipay_config['ali_public_key_path']= 'source/plugin/aljbd/key/alipay_public_key.pem';


//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


//签名方式 不需修改
$alipay_config['sign_type']    = 'MD5';

//字符编码格式 目前支持 gbk 或 utf-8
$alipay_config['input_charset']= 'utf8';

//ca证书路径地址，用于curl中ssl校验
//请保证cacert.pem文件在当前文件夹目录中
$alipay_config['cacert']    = getcwd().'/source/plugin/aljbd'.'\\cacert.pem';

//访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
$alipay_config['transport']    = 'http';

require_once("source/plugin/aljbd/lib/alipay_submit.class.php");


$format = "xml";
$v = "2.0";
$req_id = date('Ymdhis');

$notify_url = $_G['siteurl'].'source/plugin/aljbd/notify_url.php';



$call_back_url = $_G['siteurl'].'source/plugin/aljbd/call_back_url.php';

$merchant_url = $_G['siteurl'].'source/plugin/aljbd/notify.php';

//卖家支付宝帐户
$seller_email = $settings['ec_account']['value'];
//必填

//商户订单号
$out_trade_no = $orderid;
//商户网站订单系统中唯一订单号，必填

//订单名称
$subject = $shop['name'];
$subject = g2u($subject);
//必填

//付款金额
$total_fee = $shop['price1']*$num;
//必填


//请求业务参数详细
$req_data = '<direct_trade_create_req><notify_url>' . $notify_url . '</notify_url><call_back_url>' . $call_back_url . '</call_back_url><seller_account_name>' . $seller_email . '</seller_account_name><out_trade_no>' . $out_trade_no . '</out_trade_no><subject>' . $subject . '</subject><total_fee>' . $total_fee . '</total_fee><merchant_url>' . $merchant_url . '</merchant_url></direct_trade_create_req>';
//必填

/************************************************************/

//构造要请求的参数数组，无需改动
$para_token = array(
		"service" => "alipay.wap.trade.create.direct",
		"partner" => trim($alipay_config['partner']),
		"sec_id" => trim($alipay_config['sign_type']),
		"format"	=> $format,
		"v"	=> $v,
		"req_id"	=> $req_id,
		"req_data"	=> $req_data,
		"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
);

//建立请求
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestHttp($para_token);
//URLDECODE返回的信息
$html_text = urldecode($html_text);

//解析远程模拟提交后返回的信息
$para_html_text = $alipaySubmit->parseResponse($html_text);

//获取request_token
$request_token = $para_html_text['request_token'];


/**************************根据授权码token调用交易接口alipay.wap.auth.authAndExecute**************************/

//业务详细
$req_data = '<auth_and_execute_req><request_token>' . $request_token . '</request_token></auth_and_execute_req>';
//必填

//构造要请求的参数数组，无需改动
$parameter = array(
		"service" => "alipay.wap.auth.authAndExecute",
		"partner" => trim($alipay_config['partner']),
		"sec_id" => trim($alipay_config['sign_type']),
		"format"	=> $format,
		"v"	=> $v,
		"req_id"	=> $req_id,
		"req_data"	=> $req_data,
		"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
);

//建立请求
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter, 'get', '&#27491;&#22312;&#21457;&#36215;&#25903;&#20184;&#23453;&#20132;&#26131;&#35831;&#27714;&#65292;&#35831;&#31245;&#31561;&#46;&#46;&#46;');
echo $html_text;

function g2u($a) {
   return is_array($a) ? array_map('g2u', $a) : diconv($a, CHARSET, 'UTF-8');
}

function u2g($a) {
	return is_array($a) ? array_map('u2g', $a) : diconv($a, 'UTF-8', CHARSET);
}

?>